import {SAVESONGLISTDETAIL, SAVESINGERDETAIL, ADDPLAYSONG, CHANGECURRENTPLAYSONG, CHANGEMUSICLISTSTATE, INITMUSICLISTSTATE, DELSONG, DELALLSONG, ADDALLSONGTOMUSICLIST, SAVEPLAYSONGCURRENTTIME} from "./mutations-types"

export default {
  [SAVESONGLISTDETAIL](state, payload) {
    state.songListDetail = payload
  },
  [SAVESINGERDETAIL](state, payload) {
    state.singerListDetail = payload
  },
  [ADDPLAYSONG](state, payload) {
    
    // 播放列表中没有歌曲时候进行添加歌曲
    if (Object.keys(state.currentPlaySong).length == 0) {
      state.currentPlaySong = payload
      state.songs.push(payload)
      return 
    }

    // 当要添加的音乐是当前正在播放的音乐时，不进行添加
    if (state.currentPlaySong.id == payload.id) {
      return 
    }
    

    // 将要添加的音乐是否已经添加进播放列表
    let flag = state.songs.find(ele => {
      if (ele.id == payload.id) {
        return ele
      }
    })
    

    // 把将要添加的音乐添加到正在播放音乐的后面
    let i
    state.songs.find((ele, index) => {
      if (ele.id == state.currentPlaySong.id) {
        i = index + 1
        return 
      }
    })

    // 如果将要添加的音乐已在播放列表中，不再进行添加
    if (!flag) {
      state.songs.splice(i, 0, payload)
    }
    state.currentPlaySong = payload
  },
  // 修改当前播放音乐
  [CHANGECURRENTPLAYSONG](state, payload) {
    state.currentPlaySong = payload
  },
  // 修改播放列表状态
  [CHANGEMUSICLISTSTATE](state) {
    state.musicListState = !state.musicListState
  },
  // 初始化播放列表状态
  [INITMUSICLISTSTATE](state) {
    state.musicListState = false
  },
  // 删除某首歌曲
  [DELSONG](state, payload) {
    if (state.songs.length == 1) {
      state.songs = []
      state.currentPlaySong = {}
      return
    }

    let i 
    state.songs.find((ele, index) => {
      if (ele.id == payload.id) {
        i = index
        return 
      }
    })

    if (state.currentPlaySong.id == payload.id) {
      state.songs.splice(i, 1)
      if (i == state.songs.length) {
        state.currentPlaySong = state.songs[0]
        return 
      }
      state.currentPlaySong = state.songs[i]
      return 
    }

    state.songs.splice(i, 1)
  },
  // 删除全部的列表音乐
  [DELALLSONG](state) {
    state.songs = []
    state.currentPlaySong = {}
  },
  // 将歌单的所有歌曲添加到播放列表
  [ADDALLSONGTOMUSICLIST](state, payload) {
    state.songs = payload
    state.currentPlaySong = payload[0]
  },
  [SAVEPLAYSONGCURRENTTIME](state, payload) {
    state.currentPlaySongCurrentTime = payload
  }
}